
# Set up the packages, options, and working directory path
pacman::p_load(tidyverse)
set.seed(42)
options(scipen=999)
if (Sys.info()[['user']] == "alal") {
  root = "/home/alal/Dropbox/1_Research/ElecAdminFunding/ElecAdminFundingReplication"
} else {
  root = "~/Dropbox/ElecAdminFunding/ElecAdminFundingReplication"
}

# Load function for making tex table rows
source(file.path(root, "code/tables_figures/prep_table_rows.R"))

# Bring in the battleground analysis output
load(file = file.path(root, "tmp/synth_battleground.RData"), verbose = TRUE)

# Set up the matrix for storing output
out = matrix(nrow=6, ncol=4)

# Name the rows and columns of the output matrices
estimators = c("vs_narrow", "vs_broad", "to_narrow", "to_broad")
output_components = c("te estimate", "se", "num years", "treated counties", "num counties", "n")
colnames(out) = estimators
rownames(out) = output_components

# Store the estimated effects and standard errors
out[1:2,] = cbind(battleground_narrow_synth_dem$summary_table[,1],
  battleground_broad_synth_dem$summary_table[,1],
  battleground_narrow_synth_tur$summary_table[,1],
  battleground_broad_synth_tur$summary_table[,1])

# Count the number of years and units
c = 1
for(all_ests in list(battleground_narrow_synth_dem, battleground_broad_synth_dem,
  battleground_narrow_synth_tur, battleground_broad_synth_tur)){
    est_setup = attr(all_ests$ests[[1]], "setup")
    out[3,c] = est_setup$T0 + 1
    out[4,c] = nrow(est_setup$Y) - est_setup$N0
    out[5,c] = nrow(est_setup$Y)
    out[6,c] = nrow(est_setup$Y)*ncol(est_setup$Y)
    c = c + 1
}

# Put the estimates in a tex table
cat(
"\\begin{table}[ht]",
"\\centering",
"\\caption{\\textbf{Election Administration Grants Did Not Noticeably Advantage Democrats or Increase Turnout in 2020, Battleground States.}",
"\\label{tab:battleground}}",
"\\begin{tabular}{lcc|cc}",
"\\toprule \\toprule",
" & \\multicolumn{2}{c}{Dem Vote Share (\\%)} & \\multicolumn{2}{c}{Turnout (\\%)} \\\\",
" & Close & Cook & Close & Cook \\\\",
" & States & Battlegrounds & States & Battlegrounds \\\\",
"\\midrule",
paste0("Grant Recipient in 2020", prep_table_rows(out[1,]), " \\\\"),
paste0(prep_table_rows(out[2,], se=TRUE), " \\\\[2mm]"),
paste0("Num Grant Recipients ", prep_table_rows(out[4,], wholenum=TRUE), " \\\\"),
paste0("Num Counties ", prep_table_rows(out[5,], wholenum=TRUE), " \\\\"),
paste0("Observations ", prep_table_rows(out[6,], wholenum=TRUE), " \\\\"),
"\\midrule",
"County FEs & Yes & Yes & Yes & Yes \\\\",
"Year FEs & Yes & Yes & Yes & Yes \\\\",
"County Weights & Yes & Yes & Yes & Yes \\\\",
"Year Weights & Yes & Yes & Yes & Yes \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{5}{p{.8\\textwidth}}{\\footnotesize Standard errors estimated with 1,000 county block ",
"bootstrap samples reported in parentheses. Data is a balanced panel of counties in the 8 presidential ",
"elections from 1992 to 2020. Close states are those where the winner was decided by fewer than 5 percentage points. Cook battleground states are those that the Cook Political Report identified as battlegrounds prior to election day.}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/battleground_effects_table.tex"), sep="\n")

